package com.hyst.mybatisToll;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hyst.io.DoWriteModel;
import com.hyst.tableInfo.TableModel;
import com.hyst.tool.HumpConversion;
import com.hyst.tool.StringUtil;
import com.hyst.tool.TableInfo;
import com.hyst.util.ConversionContext;
import com.hyst.util.DbConfig;
import com.hyst.util.MyConfig;

/**
 * Hello world!
 *
 */
public class App {

	public static void main(String[] args) throws SQLException, IOException {

		start();
	}

	public static void start() throws SQLException, IOException {
		//
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		ConversionContext.add("hump", new HumpConversion());
		TableInfo tableInfo = (TableInfo) context.getBean("tableInfoImpl");
		DoWriteModel writeModel = (DoWriteModel) context.getBean("doWriteModel");
		MyConfig myConfig = (MyConfig) context.getBean("myConfig");
		List<String> tableNames = new ArrayList<>();//
		if (myConfig.getCreatTableNames().size()==0) {
			tableNames = tableInfo.getTableNames();
		} else {
			tableNames.addAll(myConfig.getCreatTableNames());
			//tableNames.add(myConfig.getCreatTableName()); 
		}
		File file = new File(myConfig.getFilePth() + "/model/");
		if (myConfig.isCleaPath()&&file.exists()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				File tmp = files[i];
				String name=tmp.toString();
				if (name.endsWith(".java")) {
					tmp.delete();
				}
			}
		}
		
		for (String tableName : tableNames) {
			System.out.println(tableName);
			TableModel tableModel = tableInfo.getTableInfoByName(tableName);
			writeModel.write(tableModel);
		}
	}
}
